﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using TopArtMVC.Models;
using System.Web.Security;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;

namespace TopArtMVC.Controllers
{
    public class PostViewController : Controller
    {
        //
        // GET: /PostView/
        private ApplicationDbContext db = new ApplicationDbContext();

        PostView postView = new PostView();

        public ActionResult displayPosts(List<PostView>postViews)
        {
            return View(postView);
        }
        public ActionResult Index()
        {
            return RedirectToAction("displayPosts", postView.GetAllPosts());
        }

        public ActionResult Search(string search, string searchType)
        {
            if (searchType == null)
            {
                return RedirectToAction("displayPosts", postView.searchAll(search));
            }

            else if (searchType.Equals("Title"))
            {
                return RedirectToAction("displayPosts", postView.searchWithinTitle(search));
            }
            else if (searchType.Equals("Album"))
            {
                return RedirectToAction("displayPosts", postView.searchWithinAlbum(search));
            }

            else if (searchType.Equals("Genre"))
            {
                return RedirectToAction("displayPosts", postView.searchWithinGenre(search));
            }
/*
            else if (searchType.Equals("UserName"))
            {
                return RedirectToAction("displayPosts", postView.searchAll(search));
            }
            else if (searchType.Equals("Artist"))
            {
                return RedirectToAction("displayPosts", postView.searchAll(search));
            }

            else if (searchType.Equals("Year"))
            {
                return RedirectToAction("displayPosts", postView.searchAll(search));
            }*/
            return View();

        }

        //
        // GET: /PostView/Details/5
        public ActionResult Details(int id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Post post = db.Posts.Find(id);
            if (post == null)
            {
                return HttpNotFound();
            }
            return View(post);
        }

        //
        // GET: /PostView/Create
        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /PostView/Create
        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                Post post = new Post();
                post.PostDate = DateTime.Now;
              //  post.UserID = User.Identity.GetUserId();//user id goes here
                post.PostText = collection.GetValue("PostText").ToString();
               // post.SongID=
                //  Review review = new Review();
                Song song = new Song();
                song.Title = collection.GetValue("Title").ToString();
                song.Genre = collection.GetValue("Genre").ToString();
                song.ReleaseDate = Convert.ToDateTime(collection.GetValue("ReleseDate").ToString());
                song.URL_Link = collection.GetValue("VideoLink").ToString();
                song.Album_Name = collection.GetValue("Album").ToString();
                
                db.Posts.Add(post);
              //  db.Review.Add(review);
                db.Songs.Add(song);
                return RedirectToAction("Index");
            }
            catch
            {
                
                return View();
            }
        }

        //
        // GET: /PostView/Edit/5
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /PostView/Edit/5
        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /PostView/Delete/5
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /PostView/Delete/5
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here


                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
